Skip to content

Add /files/dags to PYTHONPATH in Breeze#61436

Closed
jason810496 wants to merge 1 commit intoapache:mainfrom
jason810496:ci/breeze/fix-files-dags-python-path
Closed

Add /files/dags to PYTHONPATH in Breeze#61436
jason810496 wants to merge 1 commit intoapache:mainfrom
jason810496:ci/breeze/fix-files-dags-python-path

Conversation

@jason810496
Copy link
Member

Why

The default dags path in Breeze will be /files/dags, but /files/dags is not in PYTHONPATH, which cause the triggers defined in Dag file are not able to import by Triggerer, and have to workaround by copying Dag file to some directories that are in PYTHONPATH. Pointed out in: #58676 (comment)

What

We should add /files/dags to PYTHONPATH in Breeze in case the triggers are defined in dags, and cause "unable to import trigger" during Triggerer runtime.

Before: /files/dags is not in PYTHONPATH

>>> import sys; import pprint; pprint.pprint(sys.path)
['',
 '/opt/airflow',
 '/opt/airflow/providers/standard/tests',
 '/usr/python/lib/python310.zip',
 '/usr/python/lib/python3.10',
# ...
 '/opt/airflow/providers/yandex/src',

After: /files/dags is now in PYTHONPATH

>>> import sys; import pprint; pprint.pprint(sys.path)
['',
 '/opt/airflow',
 '/files/dags',  # <----------------------------- added!
 '/opt/airflow/providers/standard/tests',
 '/usr/python/lib/python310.zip',
 '/usr/python/lib/python3.10',
 '/usr/python/lib/python3.10/lib-dynload',
 '/usr/python/lib/python3.10/site-packages',
 '/opt/airflow/airflow-core/src',
 '/opt/airflow/airflow-ctl/src',
 ...

@potiuk
Copy link
Member

potiuk commented Feb 4, 2026

Hmm. I think that should not be the case - at least by default when Airflow imports Dags (whether it's in Triggerer, worker or Dag Processor), it should add bundle root to the PYTHONPATH automatically - and we should avoid adding it externally- if we see a problem that it is not added, we should fix it.

@jedcunningham @ephraimbuddy - am I right ?

@jedcunningham
Copy link
Member

jedcunningham commented Feb 4, 2026

Correct (edit: mostly). In fact, triggers from Dag files was pretty broken before anyways, so we intentionally removed that ability in AF3.

edit:

whether it's in Triggerer, worker or Dag Processor), it should add bundle root to the PYTHONPATH automatically

Yes for worker and dag processor. But not for the triggerer - the single process architecture means you can't do it reliably.

@jedcunningham
Copy link
Member

See #48603.

@jason810496
Copy link
Member Author

Thanks Jarek and Jed for sharing the context.

I think there two parts we pointed out here

  1. Whether the triggers are importable in Dag files
  2. The refreshing mechanism for triggers

if we see a problem that it is not added, we should fix it.

Agreed.

For first part:

Even though it's pointed out as bad practice as This just formalizes that it's best practice to have triggers come from elsewhere on sys.path instead to avoid that problem. in #48603

From the user perspective, I would expect my triggers defined in Dag files could be imported by Triggerer, but the Triggerer doesn't respect AIRFLOW__CORE__DAGS_FOLDER currently.

In fact, triggers from Dag files was pretty broken before anyways, so we intentionally removed that ability in AF3.

We are able to restore the importing triggers from Dag files by adding sys.path.insert(0, conf.get("core", "dags_folder")) before entering Triggerer's async loop.

def run(self):
"""Sync entrypoint - just run arun in an async loop."""
signal.signal(signal.SIGINT, self._handle_signal)
signal.signal(signal.SIGTERM, self._handle_signal)
asyncio.run(self.arun())

as the triggerer is unable to account for changes without having a restart.

Then this level of problem will be second part:

Unless we refactor Triggerer to run the triggers by execution_time/task_runner instead of running them as pure asyncio coroutines, we will not be able to make the Triggerer aware of the Dag Bundle. This will need much more discussion and effort and might not be finished soon.

@jason810496
Copy link
Member Author

jason810496 commented Feb 5, 2026

Thanks again for sharing the context! I will workaround with /files/airflow-breeze-config/environment_variables.env for now, as defining triggers in Dag files is not best practice and restoring Triggerer aware of the Dag Bundle will not happen soon.

@jason810496 jason810496 closed this Feb 5, 2026
@jedcunningham
Copy link
Member

Even though it's pointed out as bad practice as This just formalizes that it's best practice to have triggers come from elsewhere on sys.path instead to avoid that problem. in #48603

From the user perspective, I would expect my triggers defined in Dag files could be imported by Triggerer, but the Triggerer doesn't respect AIRFLOW__CORE__DAGS_FOLDER currently.

Don't focus as much on my PR description, but the changes in the PR. It's not just "best practice" not to in Airflow 3, we intentionally removed the ability to have them in a dag file.

Also, don't focus on "dags folder" - you must have a multi-bundle mental model now. The "dags folder" config is simply a default for familiarity sake. It is completely valid to have a deployment that does not use [core] dags_folder at all, and any change referencing it directly should immediately raise a yellow flag :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:dev-tools backport-to-v3-1-test Mark PR with this label to backport to v3-1-test branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants